package com.bob.common;

import com.bob.common.bean.Constant;
import com.bob.common.resources.ResourcesExec;
import com.bob.common.resources.exec.DefaultExec;
import com.bob.common.resources.exec.Dependency3LibExec;
import com.bob.common.resources.exec.InsideDependencyLibExec;
import com.bob.common.utils.FileUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

import java.io.File;
import java.util.Date;

public class GitPatchGenerator {
    static Log logger = new SystemStreamLog();
    /**
     * 写入变更日志到补丁文件
     * @param patchOutPath
     */
    public static void writeChangeLogToPatchFile(File patchOutPath){
        logger.info(String.format("=== 写入变更日志 %s ", patchOutPath.getAbsolutePath()));
        FileUtil.writeChangeLogToPatchFile(patchOutPath);
    }

    public static File initPatchOutPath(File patchOutPath){
        if(DataStore.getDataStore().getPatchOutPath() == null){
            DataStore.getDataStore().setPatchOutPath(new File(patchOutPath +File.separator+DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")));
            FileUtil.mkdir(DataStore.getDataStore().getPatchOutPath());
        }
        return DataStore.getDataStore().getPatchOutPath();
    }
    /**
     * 拷贝编译后的文件到补丁目录
     * @param baseDir
     * @param outputDirectory
     * @param patchOutPath
     * @param finalName
     * @param packaging
     */
    public static void copyFileToPatchDir(File baseDir,  File outputDirectory,File patchOutPath, String finalName,String packaging){
        if(packaging.equals(Constant.FILETYPE_WAR)){
            logger.info(String.format("=== 开始复制文件 %s ", finalName));

            logger.info(String.format("=== 开始复制资源文件 %s ", finalName));
            ResourcesExec resourcesExec = new DefaultExec();
            resourcesExec.exec( baseDir, outputDirectory , patchOutPath,  finalName);

            logger.info(String.format("=== 开始复制依赖文件 %s ", finalName));
                resourcesExec = new InsideDependencyLibExec();
            resourcesExec.exec( baseDir, outputDirectory , patchOutPath,  finalName);

            logger.info(String.format("=== 开始复制第三方依赖文件 %s ", finalName));
            resourcesExec = new Dependency3LibExec();
            resourcesExec.exec( baseDir, outputDirectory , patchOutPath,  finalName);
        }else{
            logger.info(String.format("=== 跳过项目  %s  不进行补丁文件添加", finalName));
        }

    }
}
